﻿using Library.OrmEnterpriseEntity.Factor;
using Library.OrmEnterpriseEntity.TableInfo;

namespace Library.OrmEnterpriseEntity {
	///<summary>
	///</summary>
	public abstract class PersistentEntityView : GlobalEntityView {
		protected abstract PersistentEntity CreatePersistentInstance();
		protected override GlobalEntity CreateEntityInstance() {
			return CreatePersistentInstance();
		}
		///<summary>
		/// Create PersistentEntity instance
		///</summary>
		///<returns></returns>
		public new PersistentEntity CreateInstance() {
			return CreatePersistentInstance();
		}
		///<summary>
		///</summary>
		public const string RowTypeName = "RowType";
		protected PersistentEntityView(bool crossRow) {
			CrossRow = crossRow;
			RowType = AddInt16(RowTypeName, false, false);
			if (!CrossRow) _rowType = RowType;
		}
		///<summary>
		/// Get table crossed row
		///</summary>
		public bool CrossRow { get; private set; }
		///<summary>
		///</summary>
		public DbColumnView RowType { get; private set; }
		private DbColumnView _rowType;
		internal override DbColumnView ColumnRowType {
			get {
				//if (CrossRow) return null;
				return _rowType;
			}
		}
		internal override QueryExpBuilder InnerExpression(QueryBuilder builder) {
			if (CrossRow) return null;
			return new QueryExpBuilder(RowType, QueryOperator.Equal, DbContext.Instance.Context.CurrentRowType) { Statement = builder.Statement };
		}
	}
}
